/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

// import { Alert, Button } from 'antd';
import { pickedLineData } from '@amcax/base';
import { PickInputRJSFFormat } from '@app-cad/common/components/command-form/constants';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { useTools } from '@app-cad/common/hooks/useTools';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useEffect, useState } from 'react';

type FormData = {
  curve1: pickedLineData;
  curve2: pickedLineData;
};

// cmd 直纹曲面
// 目前只支持反转第一条曲线。
function CmdRuledCurface({ title, onClose, props }: CurvedCmdDialogParams) {
  const [formData, setFormData] = useState<FormData>({
    curve1: null,
    curve2: null,
  });

  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      curve1: {
        type: 'string',
        format: PickInputRJSFFormat.PickCurve,
      },
      curve2: {
        type: 'string',
        format: PickInputRJSFFormat.PickCurve,
      },
    },
  };

  const { picker } = useTools();

  useEffect(() => {
    preview();
  }, [formData]);

  useAsyncEffect(async () => {
    if (props == null) {
      setFormData({ curve1: null, curve2: null });
      return;
    }

    const params = props.params;
    const {
      curves: [curve1, curve2],
      reverse,
    } = params;

    await initEditDependency([curve1, curve2]);

    const curve1Obj = picker.generatePickObjByPickID(curve1, {
      showArrow: true,
    }) as pickedLineData;
    if (reverse) {
      curve1Obj.assisObj.reverse();
    }

    const curve2Obj = picker.generatePickObjByPickID(curve2, {
      showArrow: true,
    }) as pickedLineData;

    setFormData({
      curve1: curve1Obj,
      curve2: curve2Obj,
    });
  }, []);

  const checkAndFinishDraw = async () => {
    if (!isFormDataValid()) {
      return;
    }

    await execute(false);
    onClose();
  };

  const { initEditDependency, initingEdit, confirmCommand, clear, isLoading } =
    useCommand('cmd-fill-surface', {
      commandName: 'CreateSurface',
      editDxid: props?.shape,
      onClose,
      finishDraw: checkAndFinishDraw,
    });

  const execute = (preview: boolean) => {
    return confirmCommand(
      {
        type: 'Ruled',
        a: {
          curves: [formData.curve1.edgeId, formData.curve2.edgeId],
          reverse: formData.curve1.assisObj.isReversed,
        },
      },
      preview,
    );
  };

  const isFormDataValid = () => {
    const { curve1, curve2 } = formData;
    if (!curve1 || !curve2) {
      return false;
    }
    return true;
  };

  const preview = async () => {
    clear();

    if (!isFormDataValid()) {
      return;
    }

    await execute(true);
  };

  return (
    <PickableCommandForm
      disabled={initingEdit}
      onClose={onClose}
      title={title}
      schema={schema}
      submitLoading={isLoading}
      onChange={(v) => {
        setFormData(v);
      }}
      submitText='完成'
      onSubmit={checkAndFinishDraw}
      extraUiSchema={{
        curve1: {
          'ui:placeholder': '请输入曲线1 (可反转该曲线)',
          'ui:autofocus': true,
          'ui:options': {
            showArrow: true,
            curveReversable: true,
          },
        },
        curve2: {
          'ui:placeholder': '请输入曲线2',
          'ui:options': {
            showArrow: true,
            curveReversable: false,
          },
        },
      }}
      formData={formData}
      isFormDataValid={isFormDataValid()}
    />
  );
}

export const cmdRuledCurface: CurvedBaseCommand = {
  id: 'CmdFill',
  label: 'command.ruled-surface',
  icon: 'commands/curved/ruled-surface',
  help: {
    remark: 'tips.redo',
  },
  create: (label, onClose, props) => (
    <CmdRuledCurface
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
